package com.megvii.koala.loader;

import android.content.Context;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.megvii.corelib.loader.ScrollLoader;
import com.megvii.corelib.net.Callback;
import com.megvii.koala.constance.ErrorCode;
import com.megvii.koala.data.bean.VisitorHistoryBean;
import com.megvii.koala.net.NetAPI;
import com.megvii.koala.net.visitor.history.GetVisitorHistoryResponse;

import java.util.ArrayList;
import java.util.List;

public class VisitorHistoryLoader
        extends ScrollLoader<GetVisitorHistoryResponse, List<VisitorHistoryBean>>
        implements Response.ErrorListener
{
    private int pageSize;

    private Context context;

    private String visitorId = null;

    private String firstRecordId = null;

    private String lastRecordId = null;

    private List<VisitorHistoryBean> historyRecords = new ArrayList<>();

    public VisitorHistoryLoader(Context context, int pageSize)
    {
        super();
        this.context = context;
        this.pageSize = pageSize;
    }

    public VisitorHistoryLoader(Context context, String visitorId, int pageSize)
    {
        this(context, pageSize);
        this.visitorId = visitorId;
        this.pageSize = pageSize;
    }

    @Override
    protected String getResponseError(GetVisitorHistoryResponse response)
    {
        if (response.code == 0)
        {
            return null;
        }
        else
        {
            return ErrorCode.getDescription(response.code);
        }
    }

    @Override
    protected boolean append(GetVisitorHistoryResponse response)
    {
        List<VisitorHistoryBean> list = response.data;
        if (list != null && !list.isEmpty())
        {
            if (historyRecords.isEmpty())
            {
                firstRecordId = list.get(0).id;
            }
            lastRecordId = list.get(list.size() - 1).id;
            historyRecords.addAll(list);
            return list.size() >= pageSize;
        }
        else
        {
            return false;
        }
    }

    @Override
    protected void requestForFetchMore()
    {
        super.requestForFetchMore();
        httpRequest(visitorId, lastRecordId);
    }

    @Override
    protected void requestForTestRefresh()
    {
        super.requestForTestRefresh();
        httpRequest(visitorId, null);
    }

    private void httpRequest(String visitorId, String startId)
    {
        NetAPI.requestVisitorHistory(visitorId, startId, pageSize, this, this);
        //        Log.w("koala scroll",
        //                "request " + String.valueOf(visitorId) + " " + String.valueOf(startId));
        //        HashMap<String, String> map = new HashMap<>();
        //        if (visitorId != null)
        //        {
        //            map.put("visitor_id", visitorId);
        //        }
        //        if (startId != null)
        //        {
        //            map.put("start_id", startId);
        //        }
        //        RequestManager rm = RequestManager.getInstence(context);
        //        PropertyInfo prop = new PropertyInfo(Constant.VISITORHISTORY_URL, map, null,
        //                PropertyInfo.PAGE_TYPE_VISITORHISTORY, this, PropertyInfo.XML_READ_ONLY);
        //        ConUtil.fillHeader(context, prop);// 添加头信息
        //        rm.sendRequest(prop, true, false, true);
    }

    @Override
    protected boolean isReponseNeedRefresh(GetVisitorHistoryResponse response)
    {
        List<VisitorHistoryBean> list = response.data;
        if (list == null || list.isEmpty())
        {
            return false;
        }
        else
        {
            // Log.w("koala scroll", "scroll " + firstVisibleItem);
            return !list.get(0).id.equals(firstRecordId);
        }
    }

    @Override
    protected void clear()
    {
        super.clear();
        historyRecords.clear();
        firstRecordId = null;
        lastRecordId = null;
    }

    @Override
    protected List<VisitorHistoryBean> getData()
    {
        return historyRecords;
    }

    @Callback(clazz = GetVisitorHistoryResponse.class)
    public void onGetVisitorHistoryResponse(GetVisitorHistoryResponse response)
    {
        if (response.code == 0)
        {
            super.onResponse(response);
        }
        else
        {
            super.onError("网络连接异常, 数据加载失败！");
        }
    }

    @Override
    public void onErrorResponse(VolleyError error)
    {
        super.onError("网络连接异常, 数据加载失败！");
    }
}
